package com.neuedu.elm.view;

import com.neuedu.elm.dao.IAdminDao;
import com.neuedu.elm.dao.IBusinessDao;
import com.neuedu.elm.dao.IFoodDao;
import com.neuedu.elm.dao.Impl.AdminDaoImpl;
import com.neuedu.elm.dao.Impl.BusinessDaoImpl;
import com.neuedu.elm.dao.Impl.FoodDaoImpl;
import com.neuedu.elm.po.Admin;
import com.neuedu.elm.po.Business;
import com.neuedu.elm.po.Food;

import java.util.List;
import java.util.Scanner;

public class ManagerEntry {
    public static void managerView() {


        System.out.println("欢迎来到该程序，请选择商家登录和管理员登陆");
        System.out.println("管理员登录输入1，商家登录输入2");
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        if (a == 1) {
            System.out.println("请输入管理员账号");
            String name = sc.next();
            System.out.println("请输入管理员密码");
            String password = sc.next();

            IAdminDao adminDao = new AdminDaoImpl();
            Admin admin = adminDao.login(name, password);
            while (true) {

                if (admin.getAdminName() != null) {
                    System.out.println("欢迎" + admin.getAdminName() + "访问该程序");
                    System.out.println("输入数字执行操作，数字1：查询商家列表");
                    System.out.println("数字2：查询商家");
                    System.out.println("数字3，新建商家");
                    System.out.println("数字4：删除商家");
                    int choice = sc.nextInt();
                    switch (choice) {
                        case 1:
                            showBusinessViewBy1();
                            break;
                        case 2:
                            showBusinessViewBy();
                            break;
                        case 3:
                            Business.insertBusiness();
                            break;
                        case 4:
                            deleteBusinessView();
                            break;
                        default:
                            return;
                    }
                } else {
                    System.err.println("登陆失败，账号或密码错误");
                    return;
                }

            }

        } else if (a == 2) {
            System.out.println("请输入商家账号");
            String name = sc.next();
            System.out.println("请输入商家密码");
            String password = sc.next();
            IBusinessDao businessDao = new BusinessDaoImpl();
            Business business = businessDao.login(name, password);
            while (true) {

                if (business.getBusinessName() != null) {
                    System.out.println("欢迎" + business.getBusinessName() + "访问该程序");
                    System.out.println("输入数字执行操作，数字1：查询商家信息");
                    System.out.println("数字2：修改商家信息");
                    System.out.println("数字3，修改商家密码");
                    System.out.println("数字4，查询菜品信息");
                    System.out.println("数字5，添加菜品信息");
                    System.out.println("数字6，修改菜品信息");
                    System.out.println("数字7，删除菜品信息");
                    int choice = sc.nextInt();
                    switch (choice) {
                        case 1:
                            queryBusinessView(business.getBusinessId());
                            break;
                        case 2:
                            updateBusinessView(business.getBusinessId());
                            break;
                        case 3:
                            updateBusinessPwdView(business.getBusinessId());
                            break;
                        case 4:
                            findAllFood(business.getBusinessId());
                            break;
                        case 5:
                            addFood(business.getBusinessId());
                            break;
                        case 6:
                            updateFood(business.getBusinessId());
                            break;
                        case 7:
                            deleteFood(business.getBusinessId());
                            break;
                        default:
                            return;
                    }
                } else {
                    System.err.println("登陆失败，账号或密码错误");
                    return;
                }
            }

        }

    }

    //搜索商家
    private static void showBusinessViewBy1() {

        IBusinessDao businessDao = new BusinessDaoImpl();
        List<Business> businessList = businessDao.selectBusiness(null, null);
        for (Business b : businessList
        ) {//打印一下
            System.out.println(b);
        }
    }

    //3.条件搜索商家
    private static void showBusinessViewBy() {
        String name = null;
        String address = null;
        System.out.println("是否按照名字查询? Y?N");
        Scanner sc = new Scanner(System.in);
        String choice = sc.next();
        if (choice.equals("Y")) {
            System.out.println("请输入商家名字");
            name = sc.next();
        }
        System.out.println("是否按照地址查询? Y?N");
        choice = sc.next();
        if (choice.equals("Y")) {
            System.out.println("请输入商家地址");
            address = sc.next();
        }
        IBusinessDao businessDao = new BusinessDaoImpl();
        List<Business> businessList = businessDao.selectBusiness(name, address);
        for (Business b : businessList
        ) {//打印一下
            System.out.println(b);
        }
    }

    //删除商家
    private static void deleteBusinessView() {
        System.out.println("请输入商家id");
        Scanner sc = new Scanner(System.in);
        int id = sc.nextInt();
        IBusinessDao businessDao = new BusinessDaoImpl();
        if (businessDao.deleteBusiness(id) != 0) {
            System.out.println("删除成功");
        } else System.err.println("删除失败，该id可能不存在");
    }

    //查询商家信息
    private static void queryBusinessView(int id) {
        //您的信息是
        System.out.println("您的信息是");
        IBusinessDao businessDao = new BusinessDaoImpl();
        Business business = businessDao.queryBusiness(id);

        System.out.println(business.toString1());
    }

    //修改商家信息
    private static void updateBusinessView(int id) {
        Business business = new Business();
        business.setBusinessId(id);
        Scanner sc = new Scanner(System.in);
        System.out.println("是否修改名称? Y?N");
        String choice = sc.next();
        if (choice.equals("Y")) {
            System.out.println("请输入要修改的名称");
            business.setBusinessName(sc.next());
        }
        System.out.println("是否修改地址? Y?N");
        choice = sc.next();
        if (choice.equals("Y")) {
            System.out.println("请输入要修改的名称");
            business.setBusinessAddress(sc.next());
        }
        System.out.println("是否修改描述? Y?N");
        choice = sc.next();
        if (choice.equals("Y")) {
            System.out.println("请输入要修改的描述");
            business.setBusinessExplain(sc.next());
        }
        System.out.println("是否修改起送费? Y?N");
        choice = sc.next();
        if (choice.equals("Y")) {
            System.out.println("请输入要修改的起送费");
            business.setStarPrice(sc.nextFloat());
        }
        System.out.println("是否修改配送费? Y?N");
        choice = sc.next();
        if (choice.equals("Y")) {
            System.out.println("请输入要修改的配送费");
            business.setDeliveryPrice(sc.nextFloat());
        }
        IBusinessDao businessDao = new BusinessDaoImpl();
        businessDao.updateBusiness(business);
    }

    //修改密码
    private static void updateBusinessPwdView(int businessId) {
        System.out.println("请输入要修改的密码");
        Scanner sc = new Scanner(System.in);
        String newpwd = sc.next();
        System.out.println("请再次输入要修改的密码");
        if (!newpwd.equals(sc.next()))
            System.out.println("两次密码不相同，请重新操作");
        IBusinessDao businessDao = new BusinessDaoImpl();
        businessDao.updateBusinessPwd(businessId, newpwd);
    }

    //查询菜品信息
    private static void findAllFood(int id) {
        System.out.println("正在查询菜品信息");
        IFoodDao foodDao = new FoodDaoImpl();
        foodDao.findAllFood(id);
    }

    //添加菜品信息
    private static void addFood(int id) {
        Scanner sc = new Scanner(System.in);
        System.out.println("添加菜品信息");
        System.out.println("请输入所添加的菜品名称");
        Food food = new Food();
        food.setFoodName(sc.next());
        System.out.println("请输入所添加的菜品描述");
        food.setFoodExplain(sc.next());
        System.out.println("请输入所添加的菜品价格");
        food.setFoodPrice(sc.nextFloat());
        food.setBusinessId(id);
        IFoodDao foodDao = new FoodDaoImpl();
        if (foodDao.addFood(food) != 0) System.out.println("添加成功");
    }

    //修改菜品信息
    private static void updateFood(int id) {
        Food food = new Food();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入菜品名称(我们将修改这个菜品的信息)");
        String name = sc.next();
        System.out.println("您是否需要修改的菜品名称 ? Y?N ");
        String choice = sc.next();
        if (choice.equals("Y")) {
            System.out.println("请输入修改后的菜品名称");
            food.setFoodName(sc.next());
        }
        System.out.println("您是否需要修改的菜品描述 ? Y?N ");
        choice = sc.next();
        if (choice.equals("Y")) {
            System.out.println("请输入修改后的菜品描述");
            food.setFoodExplain(sc.next());

        }
        System.out.println("您是否需要修改的菜品价格 ? Y?N ");
        choice = sc.next();
        if (choice.equals("Y")) {
            System.out.println("请输入修改后的菜品价格");
            food.setFoodPrice(sc.nextFloat());
        }
        food.setBusinessId(id);
        IFoodDao foodDao = new FoodDaoImpl();
        if (foodDao.updateFood(food, name) != 0) System.out.println("修改成功");
        else System.out.println("亲，十分抱歉，库中没有您说的菜品");
    }

    //删除菜品
    private static void deleteFood(int id) {
        Food food = new Food();
        food.setBusinessId(id);
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入想要删除的商品 ");
        String name = sc.next();
        System.out.println("您确定真的要删除" + name + " ? Y?N ");
        String choice = sc.next();
        if (choice.equals("Y")) {
            food.setFoodName(name);
            IFoodDao foodDao = new FoodDaoImpl();
            if (foodDao.deleteFood(food) != 0) {
                System.out.println("菜品" + name + "删除成功");
            } else System.out.println("菜品删除失败，您要删除的" + name + "可能不存在");

        }
    }

    public static void main(String[] args) {
        managerView();
    }
}
